
// <copyright file="CircularDataStructureEnumerator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The circular data structure enumerator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Generic
{
    using System;

    /// <summary>
    /// The circular data structure enumerator.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    [Serializable]
    public class CircularDataStructureEnumerator<T> : DataStructureEnumerator<T>
        where T : class
    {
        #region Fields

        /// <summary>
        /// The circular data structure.
        /// </summary>
        protected IDataStructureAdapter<T> circularDataStructure;

        /// <summary>
        /// The last written index.
        /// </summary>
        protected int lastWrittenIndex = -1;

        /// <summary>
        /// The in current index crossed.
        /// </summary>
        private bool inCurrentIndexCrossed;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="CircularDataStructureEnumerator{T}"/> class.
        /// </summary>
        /// <param name="store">
        /// The store.
        /// </param>
        /// <param name="startIndex">
        /// The start index.
        /// </param>
        /// <param name="lastWrittenIndex">
        /// The last written index.
        /// </param>
        /// <param name="isCycleCompleted">
        /// The is cycle completed.
        /// </param>
        public CircularDataStructureEnumerator(IDataStructureAdapter<T> store, int startIndex, int lastWrittenIndex, bool isCycleCompleted)
            : base(store, startIndex, lastWrittenIndex)
        {
            this.circularDataStructure = store;
            this.lastWrittenIndex = lastWrittenIndex;
            this._indexInt = isCycleCompleted ? this.lastWrittenIndex - 1 : startIndex - 1;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The move next.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool MoveNext()
        {
            if ((this._indexInt == -1 && this.lastWrittenIndex == -1) || this.inCurrentIndexCrossed)
            {
                return false;
            }

            this._indexInt = (this._indexInt + 1) % this._dataStructure.Capacity;

            // while (_indexInt != lastWrittenIndex && circularDataStructure[_indexInt] == null)
            // {
            // _indexInt = (_indexInt + 1)%_dataStructure.Capacity;
            // }
            if (this._indexInt == this.lastWrittenIndex && !this.inCurrentIndexCrossed)
            {
                this.inCurrentIndexCrossed = true;
            }

            return true;
        }

        #endregion Public Methods and Operators
    }
}